package algorithm.array;

import java.util.HashMap;
import java.util.Map;

/**
 * 只出现一次的数字
 * Example 1:
 *
 * Input: nums = [2,2,1]
 * Output: 1
 *
 * Input: nums = [4,1,2,1,2]
 * Output: 4
 */
public class SingleNumber {

    public static void main(String[] args) {
        int [] arr = {2,3,2,4,4};

        System.out.println(singleNumber(arr));
    }

    public static int singleNumber(int[] nums) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();

        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])){
                map.put(nums[i], map.get(nums[i]) + 1);
            }else{
                map.put(nums[i], 1);
            }
        }
        final int[] a = {1};

        map.forEach((key, value) -> {
            if (value == 1){
                a[0] = key;
            }
        });
        return a[0];
    }
}
